using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LibGreen.Jabber.ProtocolElements
{
    /// <summary>
    /// Describes the &lt;starttls&gt; element
    /// </summary>
    class StartTlsElement
    {
        /// <summary>
        /// The xmlns of the &lt;starttls&gt; element
        /// </summary>
        private static string XMLNS = "urn:ietf:params:xml:ns:xmpp-tls";

        /// <summary>
        /// Creates a new &lt;starttls&gt; element originating from the client
        /// </summary>
        /// <returns>A new &lt;starttls&gt; element</returns>
        public static Element Create()
        {
            Dictionary<string, string> attributes = new Dictionary<string, string>(2);
            attributes["xmlns"] = XMLNS;
            return Element.CreateElement("starttls", attributes);
        }

        private bool required;

        /// <summary>
        /// Initializes a StartTlsElement from a received <see cref="Element"/>
        /// </summary>
        /// <param name="element">The received <see cref="Element"/> containing the &lt;starttls&gt; stanza</param>
        public StartTlsElement(Element element)
        {
            // Parse the subtree and look for a <required/> element
            foreach (Element child in element.Children)
            {
                if (child.Name.ToLowerInvariant() == "required")
                {
                    required = true;
                    break;
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether TLS is required on the connected server
        /// </summary>
        public bool Required
        {
            get { return required; }
        }
    }
}
